package com.neusoft.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.neusoft.bean.AdminInfoBean;
import com.neusoft.dao.AdminInfoDao;

public class TestDriver {

	public static void main(String[] args) {
		test03();
	}

	private static void test03() {
		// 获取dao层接口的实现类的实例对象进行查询操作
		AdminInfoDao adminD = getMyMapper(AdminInfoDao.class);
		// 调用dao层的方法进行数据库操作
		AdminInfoBean adminB = adminD.selectAdminInfoById2(1);
		if (adminB != null) {
			System.out.println(adminB.toString());
		}
	}

	private static void test02() {
		// 获取SqlSession 对象
		SqlSession sqlS = getSession();
		// 获取dao层接口的实现类的实例对象进行查询操作
		AdminInfoDao adminD = sqlS.getMapper(AdminInfoDao.class);
		// 调用dao层的方法进行数据库操作
		List<AdminInfoBean> lstObjs = adminD.selectAdminInfoById(1);
		for (AdminInfoBean object : lstObjs) {
			System.out.println(object.toString());
		}
	}

	private static void test01() {
		// 获取SqlSession 对象
		SqlSession sqlS = getSession();
		// 执行配置的sql语句，进行查询操作
		List<Object> lstObjs = sqlS.selectList("selectAdminInfoById", 1);
		for (Object object : lstObjs) {
			System.out.println(object.toString());
		}
	}

	/**
	 * 获取Mapper对象
	 * 
	 * @param <T>
	 * 
	 * @return Mapper对象
	 */
	private static <T> T getMyMapper(Class<T> clazz) {
		T re_t = null;
		try {
			// 将mybatis的配置文件读取为输入流对象
			InputStream inConfigMybaits = TestDriver.class.getClassLoader()
					.getResourceAsStream("config-mybatis.xml");
			// 将数据库配置文件读取为Properties
			Properties propDB = new Properties();
			// 加载配置文件
			propDB.load(TestDriver.class.getClassLoader().getResourceAsStream("db.properties"));

			// SqlSessionFactory的创建类
			SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
			// 使用builder创建SqlSessionFactory类
			SqlSessionFactory factory = builder.build(inConfigMybaits, propDB);
			// 获取SqlSession 对象
			SqlSession sqlS = factory.openSession();
			// 获取mapper映射接口层
			re_t = sqlS.getMapper(clazz);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return re_t;
	}

	/**
	 * 获取SqlSession对象
	 * 
	 * @return SQLSession对象
	 */
	private static SqlSession getSession() {
		SqlSession sqlS = null;
		try {
			// 将mybatis的配置文件读取为输入流对象
			InputStream inConfigMybaits = TestDriver.class.getClassLoader().getResourceAsStream("config-mybatis.xml");
			// 将数据库配置文件读取为Properties
			Properties propDB = new Properties();
			// 加载配置文件
			propDB.load(TestDriver.class.getClassLoader().getResourceAsStream("db.properties"));

			// SqlSessionFactory的创建类
			SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
			// 使用builder创建SqlSessionFactory类
			SqlSessionFactory factory = builder.build(inConfigMybaits, propDB);
			// 获取SqlSession 对象
			sqlS = factory.openSession();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sqlS;
	}
}
